package vn.demand.dragon.view
{
	import flash.events.Event;
	
	import flexed.widgets.alerts.alert;
	
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import vn.demand.dragon.MarketingApplicationFacade;
	import vn.demand.dragon.view.components.QuotationLineForm;
	import vn.demand.dragon.view.components.QuotationPanel;

	public class QuotationPanelMediator extends Mediator
	{
		public static const NAME:String = "QuotationPanelMediator";
		
		private var quotationVO:Object;
		
		public function QuotationPanelMediator(quotationVO:Object, mediatorName:String=null, viewComponent:Object=null)
		{
			super(mediatorName, viewComponent);
			
			panel.viewId = this.getMediatorName();
			
			this.quotationVO = quotationVO;
			panel.label = "" + quotationVO.quote_code;
		}
		
		override public function listNotificationInterests():Array 
        {
            return [ 
            		MarketingApplicationFacade.CLOSE_QUOTATION_PANEL,
					 ];
        }
        
        override public function handleNotification( note:INotification ):void 
        {
        	switch(note.getName()) {
        		case MarketingApplicationFacade.CLOSE_QUOTATION_PANEL:
        			var viewId:String =note.getType() as String;
        			if (panel.viewId != viewId) {
        				return;
        			}
        			
        			if (panel.quotationForm.enableSave && panel.quotationLineForm.enableSave) {
        				alert.confirm("Do you want to save?", handleConfirmSave, Alert.CANCEL);
        			} else if (panel.quotationForm.enableSave) {
        				alert.confirm("Do you want to save?", handleConfirmSave, Alert.CANCEL);
        			} else if (panel.quotationLineForm.enableSave) {
        				alert.confirm("Do you want to save?", handleConfirmSave, Alert.CANCEL);
        			} else {
        				sendNotification(MarketingApplicationFacade.CLOSE_FINISH_QUOTATION_PANEL, quotationVO);
        			}
        			break;
        	}
        }
        
        private function handleConfirmSave(event:CloseEvent) {
        	if (event.detail == Alert.CANCEL) {
        		return;
        	}
        	if (event.detail == Alert.NO) {
        		sendNotification(MarketingApplicationFacade.CLOSE_FINISH_QUOTATION_PANEL, quotationVO);
        		return;
        	}
        	var canClose:Boolean = true;
        	var m:QuotationLineFormMediator = facade.retrieveMediator(QuotationLineFormMediator.makeId(quotationVO)) as QuotationLineFormMediator;
        	if (m.quotationLineForm.enableSave) {
	        	if (!m.doSaveQItems(new Event(QuotationLineForm.SAVE_QITEMS))) {
	        		canClose = false;
	        	}
        	}
        	var mf:QuotationFormMediator = facade.retrieveMediator(QuotationFormMediator.makeId(quotationVO)) as QuotationFormMediator;
        	if (mf.quotationForm.enableSave) {
        		if (!mf.doSaveQuotation()) {
        			canClose = false;
        		}
        	}
        	if (canClose) {
        		sendNotification(MarketingApplicationFacade.CLOSE_FINISH_QUOTATION_PANEL, quotationVO);        		
        	}
        } 
		
		public function get panel():QuotationPanel {
			return viewComponent as QuotationPanel;
		}
		
		public static function makeId(quotationVO:Object):String {
			return QuotationPanelMediator.NAME + "::" + quotationVO.quote_code;
		}
		
	}
}